package com.lhoverc.service.announcement.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lhoverc.annotation.AddDateTime;
import com.lhoverc.mapper.AnnouncementMapper;
import com.lhoverc.mapper.UserMapper;
import com.lhoverc.pojo.PageBean;
import com.lhoverc.pojo.VO.AnnouncementVO;
import com.lhoverc.pojo.VO.RecentAnnounceVO;
import com.lhoverc.pojo.dto.AnnouncementDto;
import com.lhoverc.pojo.entity.AnnouncementEntity;
import com.lhoverc.service.announcement.AnnouncementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: LiCheng
 * @CreateTime: 2025-04-20
 * @Description:
 * @Version: 1.0
 */
@Service
@Slf4j
public class AnnouncementServiceImpl implements AnnouncementService {

    @Autowired
    private AnnouncementMapper announcementMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 显示最近3条公告
     *
     * @return
     */
    @Override
    public PageBean getRecentAnnouncement() {
        //1.设置分页参数
        PageHelper.startPage(1, 3);
        //2.执行查询
        List<RecentAnnounceVO> recentAnnounceVOList = announcementMapper.pageRecentAnnouncement();
        Page<RecentAnnounceVO> p = (Page<RecentAnnounceVO>) recentAnnounceVOList;
        //3.封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    @Override
    public PageBean page(Integer page, Integer pageSize, String keyword) {
        //1.设置分页参数
        PageHelper.startPage(page, pageSize);
        //2.执行查询
        List<RecentAnnounceVO> recentAnnounceVOList = announcementMapper.page(keyword);
        Page<RecentAnnounceVO> p = (Page<RecentAnnounceVO>) recentAnnounceVOList;
        //3.封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    @Override
    public List<AnnouncementVO> getAllAnnouncement() {
        List<AnnouncementVO> announcementVOList = new ArrayList<>();
        announcementMapper.getAllAnnouncement().forEach(announcementVO -> {
            announcementVO.setCreatorName(userMapper.getUsernameById(announcementVO.getCreatorId()));
            announcementVOList.add(announcementVO);
        });
        return announcementVOList;
    }

    @AddDateTime
    @Override
    public void addAnnouncement(AnnouncementDto announcementDto) {
        AnnouncementEntity announcementEntity = new AnnouncementEntity();
        BeanUtils.copyProperties(announcementDto, announcementEntity);
        announcementEntity.setCreateTime(LocalDateTime.now());
        announcementEntity.setUpdateTime(LocalDateTime.now());
        announcementMapper.addAnnouncement(announcementEntity);
    }

    @Override
    public void deleteAnnouncement(int id) {
        announcementMapper.deleteAnnouncement(id);
    }

    @Override
    public void updateAnnouncement(AnnouncementEntity announcementEntity) {
        announcementEntity.setUpdateTime(LocalDateTime.now());
        announcementMapper.updateAnnouncement(announcementEntity);
    }
}
